package com.huchao.www.service.impl;

import com.github.pagehelper.PageHelper;
import com.huchao.www.dao.AdminMapper;
import com.huchao.www.exception.MyException;
import com.huchao.www.pojo.AdminInfo;
import com.huchao.www.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @BelongsProject: springMVC
 * @BelongsPackage: com.huchao.www.service.impl
 * @Author: 亖思
 * @CreateTime: 2022/12/12 15:19
 * @Version: 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 〈新增管理员〉
     *
     * @param admin :管理员实体
     * @Author: 亖思
     * 
     * @Date: 2022/12/12 15:27
     * @return: boolean
     **/
    @Override
    public boolean addAdmin(AdminInfo admin) throws MyException {
        if(adminMapper.isLoginNameRepeat(admin.getLoginName()) != null){
            //重复
            throw new MyException("登录名已存在");
        }else{
            //不重复就保存
            return adminMapper.saveAdmin(admin);
        }
    }

    /**
     * 〈单条软删除〉
     * @param adminId:id
     * @param nowTime:删除时间
     * @Author: 亖思
     * @Date: 2022/12/18 16:30
     * @return: boolean
     **/
    @Override
    public boolean deleteAdmin(Long adminId, Date nowTime) {
        return adminMapper.deleteByAdminId(adminId,nowTime);
    }

    /**
     * 〈多条软删除〉
     *
     * @param ids :id集合
     * @param nowTime :删除时间
     * @Author: 亖思
     * @Date: 2022/12/18 16:30
     * @return: void
     **/
    @Override
    public void deleteAdmins(List<String> ids, Date nowTime) {
        for (String id : ids){
            adminMapper.deleteByAdminId(Long.parseLong(id),nowTime);
        }
    }

    /**
     * 〈根据id查找〉
     *
     * @param adminId id
     * @Author: 亖思
     * @Date: 2022/12/17 20:37
     * @return: AdminInfo
     **/
    @Override
    public AdminInfo findById(Long adminId) {
        return adminMapper.findByAdminId(adminId);
    }

    /**
     * 〈根据用户名查询〉
     *
     * @param loginName :
     * @Author: 亖思
     * @Date: 2023/1/13 21:51
     * @return: AdminInfo
     **/
    @Override
    public AdminInfo findByUserName(String loginName) {
        return adminMapper.findByUserName(loginName);
    }

    /**
     * 〈修改管理员信息〉
     *
     * @param admin :管理员实体
     * @Author: 亖思
     * @Date: 2022/12/12 15:40
     * @return: boolean
     **/
    @Override
    public boolean updateAdmin(AdminInfo admin) {
        return adminMapper.updateAdmin(admin.getPassword(),admin.getPhone(),admin.getEmail(),admin.getProfile(),admin.getLoginName());
    }

    /**
     * 〈分页查询〉
     *
     * @param map:开始日、截止日、用户名的map
     * @param pageNum:页数
     * @param pageSize:页面容量
     * @Author: 亖思
     * @Date: 2022/12/12 16:45
     * @return: List<AdminInfo>
     **/
    @Override
    public List<AdminInfo> findAdminPageList(HashMap<String, Object> map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        return adminMapper.findAdminPageList(map);
    }
}
